package org.szy.plugin.sys.controller;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.sys.entity.SysPosition;
import org.szy.plugin.sys.param.SysPositionQueryParam;
import org.szy.plugin.sys.service.SysPositionService;

/**
 * 系统职位控制器
 *
 * @author wangbuhao
 * @since 2023-06-19 20:13:46
 */
@Controller
@Mapping("/sys/position")
public class SysPositionController extends BaseController<SysPosition> {

    private final String PATH_PREFIX = "sys/position/";
    @Inject
    private SysPositionService sysPositionService;

    /**
     * 系统职位列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统职位编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 系统职位详情页
     */
    @Get
    @Mapping("/detail")
    public ModelAndView detail() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "detail");
        return view;
    }

    /**
     * 获取系统职位分页数据
     *
     * @author wangbuhao
     * @since 2023-06-19 20:13:46
     */
    @Get
    @Mapping("/page")
    public R<Page<SysPosition>> page(SysPositionQueryParam param) {
        return R.data(sysPositionService.page(getPage(), param));
    }

    /**
     * 获取系统职位列表
     *
     * @param param 参数
     * @return 职位列表
     */
    @Get
    @Mapping("/list")
    public R<?> list(SysPositionQueryParam param) {
        QueryWrapper qw = sysPositionService.qwByQuery(param);
        return R.data(sysPositionService.list(qw));
    }

    /**
     * 获取系统职位详情
     *
     * @author wangbuhao
     * @since 2023-06-19 20:13:46
     */
    @Get
    @Mapping("/getInfo")
    public R<SysPosition> getInfo(@NotNull Long id) {
        return R.asNull(sysPositionService.getById(id));
    }

    /**
     * 添加系统职位
     *
     * @author wangbuhao
     * @since 2023-06-19 20:13:46
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    public R<?> add(@Validated SysPosition entity) {
        boolean save = sysPositionService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统职位
     *
     * @author wangbuhao
     * @since 2023-06-19 20:13:46
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    public R<?> update(@Validated SysPosition entity) {
        boolean update = sysPositionService.updateById(entity);
        return R.bool(update);
    }

    /**
     * 删除系统职位
     *
     * @author wangbuhao
     * @since 2023-06-19 20:13:46
     */
    @Delete
    @Mapping("/delete")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysPositionService.removeById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改状态
     *
     * @param param 公共状态参数
     * @return 修改结果
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(CommonStatusParam param) {
        boolean status = sysPositionService.modifyStatus(param);
        return R.bool(status);
    }

}
